//
//  DLRecordListVC.m
//  AllPass
//
//  Created by apple on 2021/11/3.
//

#import "DLRecordListVC.h"
#import "DLAddPswVC.h"
#import "DLRecordListCell.h"
@interface DLRecordListVC ()<UITextFieldDelegate>
@property(nonatomic,retain)NSMutableArray * recordArr;
@property(nonatomic,retain)UITextField * searchTF;
@end

@implementation DLRecordListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rightBtnImage = Image(@"添加_首页");
    self.myTitle = self.group.name;
    self.recordArr = [NSMutableArray new];
    self.view.backgroundColor =UnderLineColor;
    [self getData];
    [self createUI];
}
-(void)getData{
    [self.recordArr addObjectsFromArray:self.group.entries];
    [self.myTab reloadData];
}
-(void)searchWithText:(NSString*)text{
    [self.recordArr removeAllObjects];
    [DatabaseDocument searchGroup:self.group searchText:text results:self.recordArr];
    [self.myTab reloadData];
    [self.searchTF resignFirstResponder];
}
- (void)rightBtnClick{
    DLAddPswVC * vc = [DLAddPswVC new];
    vc.group = self.group;
    vc.reloadBlock = ^{
        [self.recordArr removeAllObjects];
        [self getData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.recordArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DLRecordListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DLRecordListCell"];
    if (!cell) {
        cell = [[DLRecordListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DLRecordListCell"];
    }
    if (indexPath.row < self.recordArr.count) {
        cell.model = self.recordArr[indexPath.row];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.recordArr.count) {
        KdbEntry * mo = self.recordArr[indexPath.row];
        CGFloat hei = [tableView cellHeightForIndexPath:indexPath model:mo keyPath:@"model" cellClass:[DLRecordListCell class] contentViewWidth:SCREEN_WIDTH];
        return ceilf(hei);
    }
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * header = [UIView new];
    header.backgroundColor = UnderLineColor;
    UIView * bgView = [UIView new];
    bgView.backgroundColor = UIColorWhite;
    bgView.cornerRadius = 5;
    [header addSubview:bgView];
    bgView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(10, 10, 0, 10));
    UIImageView * img = [[UIImageView alloc]initWithImage:Image(@"放大镜")];
    [bgView addSubview:img];
    img.sd_layout.leftSpaceToView(bgView, 5).centerYEqualToView(bgView).widthIs(20).heightEqualToWidth();
    self.searchTF = [UITextField new];
    self.searchTF.font = UIFontMake(16);
    self.searchTF.textColor = TextColor;
    self.searchTF.placeholderColor = UnderLineColor;
    self.searchTF.returnKeyType = UIReturnKeySearch;
    self.searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.searchTF.delegate = self;
    self.searchTF.placeholder = NSLocalizedString(@"Input key words to search", nil);
    [bgView addSubview:self.searchTF];
    [self.searchTF addTarget:self action:@selector(textFieldDidChangeValue:)  forControlEvents:UIControlEventEditingChanged];
    self.searchTF.sd_layout.leftSpaceToView(img, 5).rightSpaceToView(bgView, 0).topEqualToView(bgView).bottomEqualToView(bgView);
    return header;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length == 0) {
        return NO;
    }
    [self searchWithText:textField.text];
    return YES;
}
- (void)textFieldDidChangeValue:(UITextField*)textField{
    UITextRange * selectRange = [textField markedTextRange];
    NSString * newText = [textField textInRange:selectRange];
    if (newText.length > 0) {
        return;
    }
    if (textField.text.length == 0) {
        [self.recordArr removeAllObjects];
        [self getData];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.recordArr.count) {
        KdbEntry * e = self.recordArr[indexPath.row];
        DLAddPswVC * vc = [DLAddPswVC new];
        vc.group = self.group;
        vc.entry = e;
        vc.reloadBlock = ^{
            [self.recordArr removeAllObjects];
            [self getData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }    
}
-(void)createUI{
    self.myTab = [[UITableView alloc]initWithFrame:Frame(0, kTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kTopHeight - KTabbarSafeBottomMargin) style:UITableViewStyleGrouped];
    self.myTab.delegate = self;
    self.myTab.dataSource = self;
    self.myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTab];
}

@end
